package hc.bre

import static org.junit.jupiter.api.Assertions.*

import org.junit.jupiter.api.Test

import hc.bre.data.encoder.EncoderDefinition
import hc.bre.data.encoder.EncoderItem

@groovy.transform.CompileStatic
@groovy.transform.ToString(includeNames=true,includePackage=false)
class EncoderTest {

	@Test
	void createItem() {
		EncoderItem ei = new EncoderItem();
	}

	@Test
	void load() {
		EncoderItem ei = new EncoderItem().load([id:1,code:'c1',name:'n1']);
		assert ei.id ==1
		assert ei.code =='c1'
		assert ei.name =='n1'
	}

	@Test
	void loadEncoderItemFromList() {
		EncoderItem ei;
		ei = new EncoderItem().load([id:1,code:'c1',name:'n1']);
		assert ei.list == null;

		ei = new EncoderItem().load([id:1,code:'c1',name:'n1',list:[]]);
		assert ei.list.size() == 0;

		ei = new EncoderItem().load([id:1,code:'c1',name:'n1',
			list:[[id:11,code:'c11',name:'n11'], [id:12,code:'c12',name:'n12']],]);

		assert ei.list.size() == 2;
		assert ei.list[0] instanceof EncoderItem;
		assert ei.list[0].id == 11;
		assert ei.list[0].code == 'c11';
		assert ei.list[1].name == 'n12';
	}

	@Test
	void createDefinition() {
		EncoderDefinition ruleDef = new EncoderDefinition();
		assert ruleDef.rtype == 'data:encoder'
	}

	String jsonText ='''
{
  "rid" : 265,
  "rtype" : "data:encoder",
  "rname" : "欺诈决策",
  "rpath" : "xsxdjc/QiZhaJueCe",
  "rversion" : 12,
  "rmemo" : "",
  "rtest" : "{}",
  "rmaxid" : 8,
  "depth" : 99,
  "list" : [ {
    "id" : 1,
    "code" : "R00",
    "name" : "无欺诈",
    "list" : null
  }, {
    "id" : 2,
    "code" : "R01",
    "name" : "风险拒绝",
    "list" : [ {
      "id" : 7,
      "code" : "A04",
      "name" : "内部灰名单",
      "list" : null
    }, {
      "id" : 8,
      "code" : "A05",
      "name" : "外部灰名单",
      "list" : null
    } ]
  }, {
    "id" : 3,
    "code" : "R02",
    "name" : "疑似欺诈",
    "list" : null
  }, {
    "id" : 4,
    "code" : "R03",
    "name" : "确认欺诈",
    "list" : [ {
      "id" : 5,
      "code" : "A01",
      "name" : "内部黑名单",
      "list" : null
    }, {
      "id" : 6,
      "code" : "A02",
      "name" : "外部黑名单",
      "list" : null
    } ]
  } ]
}
'''
	
	@Test
	void loadDefinitionFromMap() {
		EncoderDefinition ruleDef = new EncoderDefinition();
		ruleDef.load(hc.Util.jsonRead(jsonText) as Map);
		println ruleDef.buildRuleScript();
	}
	
	@Test
	void loadAndFind() {
		EncoderDefinition ruleDef = new EncoderDefinition();
		ruleDef.load(hc.Util.jsonRead(jsonText) as Map);
		assert ruleDef.find(1).code =='R00'
		assert ruleDef.find(5).code =='A01'
		assert ruleDef.find(99) == null
	}
}
